﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SAKS.Models;
using SAKS.Services;

namespace SAKS.UnitTest.ServicesTest
{
    [TestFixture]
    public class ProjectServicesTest : TestBase
    {
        [Test]
        public void IsUserInProjectTest()
        {
            Guid projectId;
            string username = "IsUserInProjectTest";
            var projectServices = new ProjectServices();

            var project = new Project
            {
                Name = "IsUserInProjectTest",
                CreatedDate = DateTime.Now,
                Users = new List<User>()
            };
            var user = new User
            {
                IsAnonymous = false,
                UserName = "IsUserInProjectTest",
                LoweredUserName = "IsUserInProjectTest",
                LastActivityDate = DateTime.Now,
                Projects = new List<Project>()
            };

            using (var session = sessionFactory.OpenSession())
            {
                projectServices.Session = session;
                using (var tx = session.BeginTransaction())
                {
                    projectId = (Guid)session.Save(project);
                    session.Save(user);
                    tx.Commit();
                }
                Assert.IsFalse(projectServices.IsUserInProject(username, projectId));
                using(var tx = session.BeginTransaction())
                {

                    user.Projects.Add(project);
                    session.Update(user);
                    tx.Commit();
                }
                Assert.IsTrue(projectServices.IsUserInProject(username, projectId));
            }
        }
    }
}
